<template>
  <div class="music-player">
    <!-- 背景层 - 显示当前歌曲封面的模糊版本 -->
    <div class="blur-bg" :style="{ backgroundImage: `url(${currentSong.cover})` }"></div>

    <!-- 顶部导航栏 -->
    <nav class="top-nav">
      <div class="logo">音乐播放器（伍子怡23215220312）</div>
      <div class="nav-items">
        <button @click="togglePlaylist" class="nav-btn">
          <i class="fa fa-list"></i> 歌曲列表
        </button>
      </div>
    </nav>

    <!-- 主内容区 -->
    <div class="main-content">
      <!-- 旋转轮盘 -->
      <div class="vinyl-container" :style="{ transform: `rotate(${rotationDeg}deg)` }">
        <div class="vinyl" :class="{ spinning: isPlaying }">
          <div class="vinyl-center"></div>
          <img :src="currentSong.cover" class="album-cover" />
        </div>
      </div>

      <!-- 音乐信息 -->
      <div class="music-info">
        <h1 class="song-title">{{ currentSong.name }}</h1>
        <p class="artist">{{ currentSong.artist }}</p>
        <p class="album">{{ currentSong.album }}</p>
      </div>
    </div>

    <!-- 歌单侧边栏 -->
    <div class="playlist-sidebar" :class="{ active: playlistVisible }">
      <div class="sidebar-header">
        <h2>我的歌单</h2>
        <button @click="togglePlaylist" class="close-btn">
          <i class="fa fa-times"></i>
        </button>
      </div>
      <div class="playlist-content">
        <ul class="playlist">
          <li
              v-for="(song, index) in playlist"
              :key="song._id"
              :class="{ 'active': index === currentTrack }"
              @click="playTrack(index)"
          >
            <div class="playlist-item">
              <img :src="song.cover" class="playlist-cover" />
              <div class="playlist-info">
                <h3 class="playlist-title">{{ song.name }}</h3>
                <p class="playlist-artist">{{ song.artist }}</p>
              </div>
              <span class="playlist-duration">{{ formatTime(song.duration) }}</span>
            </div>
          </li>
        </ul>
      </div>
    </div>

    <!-- 底部控制栏 -->
    <div class="control-bar">
      <div class="progress-container">
        <span class="time">{{ formatTime(currentTime) }}</span>
        <input
            type="range"
            v-model="progress"
            @input="seekAudio"
            min="0"
            :max="duration"
            class="progress-slider"
        />
        <span class="time">{{ formatTime(duration) }}</span>
      </div>

      <div class="player-controls">
        <button @click="prevTrack" class="control-btn">⏮</button>
        <button @click="togglePlay" class="play-btn">
          {{ isPlaying ? '⏸' : '▶' }}
        </button>
        <button @click="nextTrack" class="control-btn">⏭</button>
      </div>
    </div>
  </div>
</template>

<script lang="ts">
import { ref, reactive, computed, onMounted, onUnmounted } from "vue";

export default {
  setup() {
    // 播放器核心状态
    const audio = ref<HTMLAudioElement | null>(new Audio());
    const isPlaying = ref(false);
    const currentTrack = ref(0);
    const currentTime = ref(0);
    const duration = ref(0);
    const rotationDeg = ref(0);
    let rotateInterval: number;

    // 歌单和界面状态
    const playlist = reactive([
      {
        name: "Soda City Funk",
        artist: "Galactum",
        url: `${import.meta.env.BASE_URL}song/1.mp3`,
        cover: `${import.meta.env.BASE_URL}covers/1.jpg`,
        duration: 210,
      },
      {
        name: "Thank you for dears",
        artist: "GET IN THE RING",
        url: `${import.meta.env.BASE_URL}song/2.mp3`,
        cover: `${import.meta.env.BASE_URL}covers/2.jpg`,
        duration: 245,
      },
      {
        name: "daisy (feat. RJ Pasin)",
        artist: "ptasinski、RJ Pasin",
        url: `${import.meta.env.BASE_URL}song/3.mp3`,
        cover: `${import.meta.env.BASE_URL}covers/3.jpg`,
        duration: 198,
      },
    ]);

    const playlistVisible = ref(false);

    // 初始化时播放第一首歌曲
    onMounted(() => {
      if (audio.value) {
        audio.value.addEventListener("timeupdate", updateProgress);
        audio.value.addEventListener("ended", nextTrack);
        if (playlist.length > 0) {
          playTrack(0);
        }
      }
    });

    onUnmounted(() => {
      stopRotation();
      if (audio.value) {
        audio.value.removeEventListener("timeupdate", updateProgress);
        audio.value.removeEventListener("ended", nextTrack);
        audio.value.pause();
      }
    });

    // 计算属性
    const currentSong = computed(() =>
        playlist[currentTrack.value] || { name: "加载中", artist: "", cover: "" }
    );

    const progress = computed({
      get: () => currentTime.value,
      set: (val) => {
        if (audio.value) audio.value.currentTime = val;
      },
    });

    // 播放控制方法
    const playTrack = (index: number) => {
      if (index >= 0 && index < playlist.length) {
        currentTrack.value = index;
        if (audio.value) {
          audio.value.src = playlist[index].url;
          audio.value.play();
          isPlaying.value = true;
          startRotation();
        }
      }
    };

    const togglePlay = () => {
      if (!audio.value) return;

      if (isPlaying.value) {
        audio.value.pause();
        stopRotation();
      } else {
        audio.value.play();
        startRotation();
      }
      isPlaying.value = !isPlaying.value;
    };

    const nextTrack = () => {
      const nextIndex = (currentTrack.value + 1) % playlist.length;
      playTrack(nextIndex);
    };

    const prevTrack = () => {
      const prevIndex = (currentTrack.value - 1 + playlist.length) % playlist.length;
      playTrack(prevIndex);
    };

    // 进度控制
    const updateProgress = () => {
      if (audio.value) {
        currentTime.value = audio.value.currentTime;
        duration.value = audio.value.duration || 0;
      }
    };

    const seekAudio = (e: Event) => {
      if (audio.value) {
        audio.value.currentTime = Number((e.target as HTMLInputElement).value);
      }
    };

    // 旋转动画控制
    const startRotation = () => {
      stopRotation();
      rotateInterval = window.setInterval(() => {
        rotationDeg.value = (rotationDeg.value + 0.5) % 360;
      }, 20);
    };

    const stopRotation = () => {
      clearInterval(rotateInterval);
    };

    // 工具函数
    const formatTime = (seconds: number) => {
      if (isNaN(seconds)) return "0:00";
      const mins = Math.floor(seconds / 60);
      const secs = Math.floor(seconds % 60);
      return `${mins}:${secs < 10 ? "0" : ""}${secs}`;
    };

    // 界面控制方法
    const togglePlaylist = () => {
      playlistVisible.value = !playlistVisible.value;
    };


    return {
      audio,
      isPlaying,
      playlist,
      currentTrack,
      currentSong,
      currentTime,
      duration,
      progress,
      rotationDeg,
      playlistVisible,
      playTrack,
      togglePlay,
      nextTrack,
      prevTrack,
      updateProgress,
      seekAudio,
      formatTime,
      togglePlaylist,
    };
  },
};
</script>

<style scoped>
.music-player {
  position: relative;
  height: 100vh;
  width: 100%;
  color: white;
  overflow: hidden;
  font-family: 'Inter', sans-serif;
}

/* 模糊背景层 */
.blur-bg {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-size: cover;
  background-position: center;
  filter: blur(20px);
  z-index: -1;
  opacity: 0.5;
  transform: scale(1.1); /* 放大以避免模糊边缘 */
}

/* 顶部导航栏 */
.top-nav {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  height: 60px;
  background: rgba(0, 0, 0, 0.7);
  backdrop-filter: blur(10px);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 20px;
  z-index: 10;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.3);
}

.logo {
  font-size: 1.5rem;
  font-weight: bold;
  color: white;
  opacity: 0.5;
}

.nav-items {
  display: flex;
  gap: 15px;
}

.nav-btn {
  background: none;
  border: none;
  color: white;
  font-size: 1rem;
  cursor: pointer;
  padding: 8px 12px;
  border-radius: 5px;
  transition: all 0.3s;
}

.nav-btn:hover {
  background: rgba(255, 255, 255, 0.1);
}

/* 主内容区 */
.main-content {
  position: relative;
  height: calc(100vh - 160px); /* 减去顶部导航栏和底部控制栏的高度 */
  margin-top: 60px;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
}

/* 旋转轮盘 */
.vinyl-container {
  width: 400px;
  height: 400px;
  margin-right: 50px;
}

.vinyl {
  width: 100%;
  height: 100%;
  border-radius: 50%;
  background: linear-gradient(
      135deg,
      rgba(0, 0, 0, 0.8) 0%,
      rgba(50, 50, 50, 0.9) 50%,
      rgba(0, 0, 0, 0.8) 100%
  );
  position: relative;
  box-shadow: 0 0 20px rgba(0, 0, 0, 0.8);
  transition: transform 0.3s ease;
}

.vinyl.spinning {
  animation: spin 20s linear infinite;
}

@keyframes spin {
  from {
    transform: rotate(0deg);
  }
  to {
    transform: rotate(360deg);
  }
}

.vinyl-center {
  position: absolute;
  top: 50%;
  left: 50%;
  width: 40px;
  height: 40px;
  background: #333;
  border: 2px solid #555;
  border-radius: 50%;
  transform: translate(-50%, -50%);
  z-index: 2;
}

.album-cover {
  position: absolute;
  top: 50%;
  left: 50%;
  width: 60%;
  height: 60%;
  border-radius: 50%;
  transform: translate(-50%, -50%);
  object-fit: cover;
  z-index: 1;
  box-shadow: 0 0 10px rgba(0, 0, 0, 0.5);
}

/* 音乐信息 */
.music-info {
  max-width: 400px;
  text-align: center;
}

.song-title {
  font-size: 2.5rem;
  margin-bottom: 10px;
  text-shadow: 0 2px 4px rgba(0, 0, 0, 0.5);
}

.artist {
  font-size: 1.5rem;
  color: #aaa;
  margin-bottom: 5px;
}

.album {
  font-size: 1.2rem;
  color: #888;
}

/* 歌单侧边栏 */
.playlist-sidebar {
  position: fixed;
  top: 60px;
  right: -400px;
  width: 400px;
  height: calc(100vh - 160px);
  background: rgba(0, 0, 0, 0.8);
  backdrop-filter: blur(10px);
  transition: all 0.3s ease;
  z-index: 9;
  border-radius: 10px 0 0 10px;
  box-shadow: -5px 0 15px rgba(0, 0, 0, 0.3);
}

.playlist-sidebar.active {
  right: 0;
}

.sidebar-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 15px 20px;
  border-bottom: 1px solid rgba(255, 255, 255, 0.1);
}

.sidebar-header h2 {
  margin: 0;
  font-size: 1.5rem;
}

.close-btn {
  background: none;
  border: none;
  color: white;
  font-size: 1.2rem;
  cursor: pointer;
  transition: all 0.3s;
}

.close-btn:hover {
  transform: scale(1.2);
}

.playlist-content {
  height: calc(100% - 60px);
  overflow-y: auto;
  padding: 10px;
}

.playlist {
  list-style: none;
  padding: 0;
  margin: 0;
}

.playlist li {
  display: flex;
  align-items: center;
  padding: 10px;
  border-radius: 5px;
  margin-bottom: 5px;
  cursor: pointer;
  transition: all 0.3s;
}

.playlist li:hover {
  background: rgba(255, 255, 255, 0.1);
}

.playlist li.active {
  background: rgba(29, 185, 84, 0.2);
  color: #1db954;
}

.playlist-item {
  display: flex;
  align-items: center;
  width: 100%;
}

.playlist-cover {
  width: 50px;
  height: 50px;
  border-radius: 5px;
  object-fit: cover;
  margin-right: 15px;
  box-shadow: 0 0 5px rgba(0, 0, 0, 0.3);
}

.playlist-info {
  flex: 1;
  text-align: left;
  overflow: hidden;
}

.playlist-title {
  font-size: 1rem;
  margin: 0;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.playlist-artist {
  font-size: 0.8rem;
  color: #aaa;
  margin: 0;
}

.playlist-duration {
  font-size: 0.8rem;
  color: #888;
  min-width: 40px;
  text-align: right;
}

/* 底部控制栏 */
.control-bar {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  height: 100px;
  background: rgba(0, 0, 0, 0.7);
  backdrop-filter: blur(10px);
  display: flex;
  flex-direction: column;
  justify-content: center;
  padding: 0 20px;
  z-index: 10;
}

.progress-container {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 15px;
}

.progress-slider {
  flex: 1;
  height: 4px;
  -webkit-appearance: none;
  background: rgba(255, 255, 255, 0.3);
  border-radius: 2px;
  outline: none;
}

.progress-slider::-webkit-slider-thumb {
  -webkit-appearance: none;
  width: 14px;
  height: 14px;
  background: #1db954;
  border-radius: 50%;
  cursor: pointer;
  transition: all 0.15s ease;
}

.progress-slider::-webkit-slider-thumb:hover {
  transform: scale(1.2);
}

.time {
  font-size: 12px;
  color: #aaa;
  min-width: 40px;
  text-align: center;
}

.player-controls {
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 30px;
}

.control-btn {
  background: none;
  border: none;
  color: white;
  font-size: 20px;
  cursor: pointer;
  padding: 8px;
  opacity: 0.8;
  transition: all 0.2s;
}

.control-btn:hover {
  opacity: 1;
  transform: scale(1.1);
}

.play-btn {
  width: 50px;
  height: 50px;
  border-radius: 50%;
  background: #1db954;
  border: none;
  color: white;
  font-size: 20px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: all 0.2s;
  box-shadow: 0 0 10px rgba(29, 185, 84, 0.5);
}

.play-btn:hover {
  transform: scale(1.1);
  box-shadow: 0 0 15px rgba(29, 185, 84, 0.7);
}

/* 响应式设计 */
@media (max-width: 1024px) {
  .main-content {
    flex-direction: column;
    text-align: center;
  }

  .vinyl-container {
    margin-right: 0;
    margin-bottom: 30px;
    width: 300px;
    height: 300px;
  }

  .playlist-sidebar {
    width: 300px;
    right: -300px;
  }

  .song-title {
    font-size: 2rem;
  }

  .artist {
    font-size: 1.2rem;
  }

  .album {
    font-size: 1rem;
  }
}

@media (max-width: 768px) {
  .top-nav {
    height: 50px;
  }

  .logo {
    font-size: 1.2rem;
  }

  .nav-btn {
    font-size: 0.9rem;
    padding: 5px 10px;
  }

  .main-content {
    height: calc(100vh - 150px);
    margin-top: 50px;
  }

  .vinyl-container {
    width: 250px;
    height: 250px;
  }

  .playlist-sidebar {
    top: 50px;
    height: calc(100vh - 150px);
  }

  .control-bar {
    height: 100px;
  }

  .player-controls {
    gap: 20px;
  }

  .play-btn {
    width: 45px;
    height: 45px;
  }
}
</style>